package com.luoyuanxiangvip.web.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.luoyuanxiangvip.common.holder.RequestHolder;
import com.luoyuanxiangvip.common.utils.AjaxResult;
import com.luoyuanxiangvip.common.utils.IpUtil;
import com.luoyuanxiangvip.common.vo.BizArticleVO;
import com.luoyuanxiangvip.core.entity.BizArticle;
import com.luoyuanxiangvip.core.entity.BizArticleLove;
import com.luoyuanxiangvip.core.service.IBizArticleLoveService;
import com.luoyuanxiangvip.core.service.IBizArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 文章表 前端控制器
 * </p>
 *
 * @author luoyuanxiang <p>luoyuanxiangvip.com</p>
 * @since 2019-05-26
 */
@RestController
@Api(description = "文章管理")
public class BizArticleController{

    @Resource
    @Lazy
    private IBizArticleService iBizArticleService;
    @Resource
    @Lazy
    private IBizArticleLoveService iBizArticleLoveService;

    @PostMapping("/article/list")
    @ApiOperation(value = "获取文章列表")
    public AjaxResult articleList(@RequestBody BizArticleVO bizArticleVO) {
        IPage<BizArticle> page = iBizArticleService.findPageBreakByCondition(bizArticleVO);
        return AjaxResult.success("获取文章列表成功", page);
    }

    @GetMapping("/article/{id}")
    @ApiOperation(value = "获取文章详情")
    public AjaxResult articleInfo(@PathVariable("id") Long id) {
        BizArticle info = iBizArticleService.info(id, Boolean.TRUE);
        return AjaxResult.success("获取文章详情成功", info);
    }

    @GetMapping("/article/likes/{id}")
    @ApiOperation(value = "喜欢文章")
    public AjaxResult articleLikes(@PathVariable("id") Long id) {
        BizArticleLove love = new BizArticleLove();
        love.setArticleId(id)
                .setIp(IpUtil.getRealIp(RequestHolder.getRequest()))
                .setLookTime(new Date())
                .setCreateTime(new Date())
                .setUpdateTime(new Date());
        iBizArticleLoveService.save(love);
        return AjaxResult.success();
    }
}
